import axios from 'axios'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Router from 'vue-router'

Vue.use(Router)

const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/login',
            component: () => import('@/components/UserLogin')
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/components/UserLogin')
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('@/components/UserRegister')
        },
        {
            path: '/register/setUserInfo',
            name: 'setUserInfo',
            component: () => import('@/components/SetUserInfo')
        },
        {
            path: '/admin',
            component: () => import('@/components/admin/AdminView.vue'),
            children: [
                {
                    path: '/admin/addCourse',
                    component: () => import('@/components/admin/AddCourse.vue')
                },
                {
                    path: '/admin/updateCourse',
                    component: () => import('@/components/admin/UpdateCourse.vue'),
                },
                {
                    path: "/admin/updateCourse/courseDetial",
                    name: "adminCourseDetial",
                    component: () => import('@/components/admin/CourseDetial.vue')
                },
                {
                    path: "/admin/comment",
                    component: () => import('@/components/admin/AdminComment.vue')
                },
                {
                    path: "/admin/user",
                    component: () => import('@/components/admin/AdminUser.vue')
                },
                {
                    path: "/admin/user/update",
                    name:"adminUpdateUser",
                    component: () => import('@/components/admin/UpdateUser.vue')
                }
            ]

        },
        {
            path: '/user',
            name: 'user',
            component: () => import('@/components/user/User'),
            children: [
                {
                    path: 'home',
                    name: 'home',
                    component: () => import('@/components/user/Home')
                },
                {
                    path: 'findCourse',
                    name: 'findCourse',
                    component: () => import('@/components/user/FindCourse')
                },
                {
                    path: 'course/:courseId',
                    name: 'course:courseId',
                    component: () => import('@/components/user/CourseDetials')
                },
                {
                    path: 'courseTable',
                    name: 'courseTable',
                    component: () => import('@/components/user/CourseTable')
                },
                {
                    path: 'myCourse',
                    name: "myCourse",
                    component: () => import('@/components/user/MyCourse')
                },
                {
                    path: 'userCenter',
                    name: 'userCenter',
                    component: () => import('@/components/user/userCenter/UserCenter'),
                    children: [
                        {
                            path: 'updateUserInfo',
                            name: 'updateUserInfo',
                            component: () => import('@/components/user/userCenter/UpdateUserInfo')
                        },
                        {
                            path: 'updatePassword',
                            name: 'updatePassword',
                            component: () => import('@/components/user/userCenter/UpdatePassword')
                        },
                        {
                            path: 'myComments',
                            name: 'myComments',
                            component: () => import('@/components/user/userCenter/MyComments')
                        },
                        {
                            path: 'myAvatar',
                            name: 'myAvatar',
                            component: ()=> import('@/components/user/userCenter/MyAvatar')
                        }
                    ]
                }
            ]
        },
        {
            path: '/error/loginInfoInvalid',
            name: 'loginInfoInvalid',
            component: () => import('@/components/error/LoginInfoInvalid')
        }
    ]
})

/*
router.beforeEach((to, from, next) =>{
    if(to.path.startsWith('/login')) {
        window.sessionStorage.removeItem('access-admin')
        next()
    } else {
        let token = window.sessionStorage.getItem('access-admin')
        if(!admin) {
            next('/login')
        } else {
            axios({
                url:'',
                method:'get',
                headers:{
                    AUTHORIZATION : token
                }
            }).then((response) => {
                if(!response.data){
                    console.log('路由守卫：校验失败')
                    next('/error/loginInfoInvalid')
                }
            })
            next()
        }
    }

})
*/
export default router